/*
Minims .Net Playstation 3 UPNP ContentDirectory
Copyright (C) 2008  Guillaume Plourde

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; version 2
of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/


using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Xml.Schema;

using minims_lib;

namespace minims_win32
{
	public partial class MainDialog : Form
	{
		// Private var
		private bool ServerStarted;
		private bool ShowOptions;

		private MinimsServer Server;
		private MinimsConfig Config;

		public MainDialog()
		{
			InitializeComponent();

			btn_DisplayOptions.Text = "Show Options";
			m_StatusGroup.Visible = false;
			m_OptionGroup.Visible = false;

			this.Height = 390;

			ShowOptions = false;

			Config = new MinimsConfig();
			Config.Load(Application.StartupPath + "\\config\\");
			Config.LoadMimeTypes(Application.StartupPath + "\\config\\");
			propertyGrid1.SelectedObject = Config;

			// Add default folders
			AddFolder(Config.Path);
		}

		private void AddFolder(string dir)
		{
			if (dir != null)
			{
				string[] sep0 = { ";" };
				string[] list = dir.Split(sep0, StringSplitOptions.RemoveEmptyEntries);
				foreach (string s in list)
				{
					c_FolderList.Items.Add(s, "folder.png");
				}

				UpdateFolderList();
			}
		}
		/// <summary>
		/// Update folder list and harddrive usage information
		/// </summary>
		private void UpdateFolderList()
		{
			List<string> folders = new List<string>();
			string conf_pathList = "";
			foreach (ListViewItem item in c_FolderList.Items)
			{
				folders.Add(item.Text);
				conf_pathList += (item.Text + ";");
			}

			Config.Path = conf_pathList;
			Config.Save(Application.StartupPath + "\\config\\");

			if (Server != null)
			{
				Server.UpdateContent(folders);

				int videoSize = (int)(Server.ContentSize / 1048576);

				if (videoSize > 1024)
				{
					videoSize /= 1024;
					this.storageSpace1.Video = videoSize;
					this.m_VideoSpace.Text = (videoSize.ToString() + " GB");
					this.storageSpace1.Invalidate();
				}
				else
				{
					this.storageSpace1.Video = videoSize;
					this.m_VideoSpace.Text = (videoSize.ToString() + " MB");
					this.storageSpace1.Invalidate();
				}
			}
		}

		#region Form controls events
		private void OnClosing(object sender, FormClosingEventArgs e)
		{
			if (Server != null)
				Server.Stop();
		}
		private void c_CloseButton_Click(object sender, EventArgs e)
		{
			if (ServerStarted)
			{
				Server.Stop();
				Server = null;
				ServerStarted = false;
			}

			Application.ExitThread();
		}
		private void c_StartButton_Click(object sender, EventArgs e)
		{
			if (!ServerStarted)
			{
				Server = new MinimsServer();
				ServerStarted = Server.Start(Application.StartupPath + "\\config\\");
				c_StartButton.Text = "Stop";
				c_StartButton.Image = global::minims_win32.Properties.Resources.process_stop;

				UpdateFolderList();
			}
			else
			{
				Server.Stop();
				Server = null;
				ServerStarted = false;
				c_StartButton.Text = "Start";
				c_StartButton.Image = global::minims_win32.Properties.Resources.media_playback_start;
			}
		}
		private void c_AddFolder_Click(object sender, EventArgs e)
		{
			c_SharedFolderBrowser.ShowDialog();
			
			if (c_SharedFolderBrowser.SelectedPath != "")
				c_FolderList.Items.Add(c_SharedFolderBrowser.SelectedPath, "folder.png");

			UpdateFolderList();
		}
		private void c_RemoveFolder_Click(object sender, EventArgs e)
		{
			foreach (ListViewItem i in c_FolderList.SelectedItems)
			{
				c_FolderList.Items.Remove(i);
			}
			UpdateFolderList();
		}
		private void ApplyConfig_Click(object sender, EventArgs e)
		{
			Config.Save(Application.StartupPath + "\\config\\");
		}
		private void CancelConfig_Click(object sender, EventArgs e)
		{
			Config.Load(Application.StartupPath + "\\config\\");
			propertyGrid1.SelectedObject = Config;
		}
		private void btn_DisplayOptions_Click(object sender, EventArgs e)
		{
			if (ShowOptions)
			{
				btn_DisplayOptions.Text = "Show Options";
				m_StatusGroup.Visible = false;
				m_OptionGroup.Visible = false;

				this.Height = 390;

				ShowOptions = false;
			}
			else
			{
				btn_DisplayOptions.Text = "Hide Options";
				m_StatusGroup.Visible = true;
				m_OptionGroup.Visible = true;

				this.Height = 570;

				ShowOptions = true;
			}
		}
		#endregion
	}

	#region Storage Panel Class
	public class StorageSpace : Panel
	{
		public int Audio = 0;
		public int Video = 0;
		public int Photo = 0;
		public int Game = 0;

		public StorageSpace()
		{
		}

		public LinearGradientBrush CreateBrush(Color c0, Color c1, Color c2, Color c3)
		{
			LinearGradientBrush brush;

			Color[] gradientColors = new Color[] {
					c0,
					c1,
					c2,
					c3,
				};

			Single[] gradientPositions = new Single[] {
					0,
					0.50001f,
					0.50002f,
					1.0f
				};

			ColorBlend blend = new ColorBlend(4);
			blend.Colors = gradientColors;
			blend.Positions = gradientPositions;

			brush = new LinearGradientBrush(new Rectangle(0, 0, this.Width, this.Height), Color.FromArgb(0, 0, 0), Color.FromArgb(0, 0, 0), 90f);
			brush.InterpolationColors = blend;

			return brush;
		}

		protected override void OnPaint(PaintEventArgs e)
		{
			int pos = 0;

			e.Graphics.FillRectangle(
				CreateBrush(Color.FromArgb(255, 120, 120, 120), Color.FromArgb(255, 55, 55, 55), Color.FromArgb(255, 12, 12, 12), Color.FromArgb(255, 6, 6, 6)),
				new Rectangle(0, 0, this.Width, this.Height)
				);

			e.Graphics.FillRectangle(
				CreateBrush(Color.FromArgb(124, 161, 227), Color.FromArgb(92, 122, 190), Color.FromArgb(23, 52, 152), Color.FromArgb(50, 174, 221)),
				new Rectangle(pos, 0, (int)((float)Audio * (float)this.Width / 100.0f), this.Height)
				);

			pos += (int)((float)Audio * (float)this.Width / 100.0f);
			e.Graphics.FillRectangle(
				CreateBrush(Color.FromArgb(200, 160, 255), Color.FromArgb(190, 130, 200), Color.FromArgb(150, 60, 200), Color.FromArgb(220, 180, 220)),
				new Rectangle(pos, 0, (int)((float)Video * (float)this.Width / 100.0f), this.Height)
				);

			pos += (int)((float)Video * (float)this.Width / 100.0f);
			e.Graphics.FillRectangle(
				CreateBrush(Color.FromArgb(240, 200, 58), Color.FromArgb(255, 134, 28), Color.FromArgb(240, 100, 10), Color.FromArgb(255, 134, 28)),
				new Rectangle(pos, 0, (int)((float)Photo * (float)this.Width / 100.0f), this.Height)
				);

			pos += (int)((float)Photo * (float)this.Width / 100.0f);
			e.Graphics.FillRectangle(
				CreateBrush(Color.FromArgb(0, 250, 50), Color.FromArgb(0, 220, 50), Color.FromArgb(0, 190, 50), Color.FromArgb(80, 240, 80)),
				new Rectangle(pos, 0, (int)((float)Game * (float)this.Width / 100.0f), this.Height)
				);

			e.Graphics.DrawRectangle(Pens.DarkGray, new Rectangle(0, 0, this.Width, this.Height));
		}
	}
	#endregion
}